package com.sakura.controller;

import com.github.pagehelper.PageInfo;
import com.sakura.po.Actor;
import com.sakura.service.ActorService;
import com.sakura.service.AdminService;
import com.sakura.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
public class ActorController {
    @Autowired
    private ActorService actorService;

    @GetMapping("/actorIndex")
    public String actorIndex(){
        return "actor/actorIndex";
    }
    @GetMapping("/addActor")
    public String addActor(){ return"actor/addActor"; }

    @ResponseBody
    @RequestMapping("/addActorSubmit")
    public R addActorSubmit(@RequestBody Actor actor){
        actorService.addActorSubmit(actor);
        return R.ok();
    }

    @ResponseBody
    @RequestMapping("/actorAll")
    public R actorAll(Actor actor,
                     @RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "15") Integer limit){
        PageInfo<Actor> pageInfo=actorService.queryActorAll(actor,page,limit);
        return  R.ok("成功",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 删除功能
     */
    @ResponseBody
    @RequestMapping("/deleteActorByIds")
    public R deleteActorByIds(String ids){
        List list
                = Arrays.asList(ids.split(","));
        actorService.deleteActorByIds(list);
        return R.ok();
    }

    /**
     * 根据id查询记录信息
     */
    @GetMapping("/queryActorById")
    public String queryActorById(Model model, Integer id){
        //根据id获取对象
        Actor actor= actorService.queryActorById(id);
        model.addAttribute("actor",actor);
        return "actor/updateActor";
    }


    /**
     * 修改实现功能
     */
    @ResponseBody
    @RequestMapping("/updateActorSubmit")
    public R updateActorSubmit(@RequestBody Actor actor){
        System.out.println(actor+"************************");
        actorService.updateActorSubmit(actor);
        return R.ok();
    }






}
